package atom.core0.jvm;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import atom.core0.bean.general.MyDaemonThreadInfo;
import atom.core0.bean.general.MyRemoteServerThreadInfo;
import atom.core0.object.InterfaceUtil;
import atom.core0.util.StringUtil;

public class ThreadUtil
{
	private static Logger	log	= Logger.getLogger(ThreadUtil.class);

	// 下面的静态方法可以用数组返回Java VM中当前运行的所有线程
	public static Thread[] findAllThreads()
	{
		ThreadGroup group = Thread.currentThread().getThreadGroup();
		ThreadGroup topGroup = group;

		// 遍历线程组树，获取根线程组
		while (group != null)
		{
			topGroup = group;
			group = group.getParent();
		}
		// 激活的线程数加倍
		int estimatedSize = topGroup.activeCount() * 2;
		Thread[] slackList = new Thread[estimatedSize];
		// 获取根线程组的所有线程
		int actualSize = topGroup.enumerate(slackList);
		// copy into a list that is the exact size
		Thread[] list = new Thread[actualSize];
		System.arraycopy(slackList, 0, list, 0, actualSize);
		return list;
	}

	public static List findAllMyRemoteDispatcherServletThread()
	{
		Thread[] threads = findAllThreads();
		List list = new ArrayList();
		for (int i = 0; i < threads.length; i++)
		{
			if (threads[i].getName().equals("MyRemoteDispatcherServlet"))
				list.add(threads[i]);
		}
		return list;
	}

	public static List findAllMyRemoteDispatcherServletThreadInfo()
	{
		List list = findAllMyRemoteDispatcherServletThread();
		for (int i = 0; i < list.size(); i++)
		{
			Thread t = (Thread) list.get(i);
			MyRemoteServerThreadInfo info = new MyRemoteServerThreadInfo();
			info.setThreadId(t.getId());
			info.setThreadName(t.getName());
			
			try
			{
				Method m = t.getClass().getMethod("getActionName");
				String value = (String) m.invoke(t);
				info.setActionName(value);
				
				m = t.getClass().getMethod("getMethodName");
				value = (String) m.invoke(t);
				info.setMethodName(value);

				m = t.getClass().getMethod("getRemoteType");
				value = (String) m.invoke(t);
				info.setActionName(value);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			list.add(info);
		}
		return list;
	}

	public static Map findAllMyDaemonThreadInfo()
	{
		Thread[] threads = ThreadUtil.findAllThreads();
		Thread t = null;
		// List threadInfoList = new ArrayList();
		TreeMap threadInfoMap = new TreeMap();
		for (int i = 0; i < threads.length; i++)
		{
			if (InterfaceUtil.hasInterface(threads[i].getClass().getSuperclass(),
			        "hikefa.core.thread.iface.IMyDaemonThread"))
			{
				MyDaemonThreadInfo info = new MyDaemonThreadInfo();
				t = threads[i];
				info.setThreadId(t.getId());
				info.setThreadName(t.getName());
				String daemonThreadDesc;
				try
				{
					Method m = t.getClass().getMethod("getDaemonThreadDesc");
					daemonThreadDesc = (String) m.invoke(t);

					if (StringUtil.isEmpty(daemonThreadDesc))
						daemonThreadDesc = t.getName();
					info.setDaemonThreadDesc(daemonThreadDesc);

					m = t.getClass().getMethod("getDaemonId");
					String daemonId = (String) m.invoke(t);
					info.setDaemonId(daemonId);

					m = t.getClass().getMethod("getDaemonState");
					String daemonState = (String) m.invoke(t);
					info.setDaemonState(daemonState);
				}
				catch (Exception e)
				{
					daemonThreadDesc = t.getName();
					e.printStackTrace();
				}
				threadInfoMap.put(daemonThreadDesc + "_" + i, info);

			}
		}
		return threadInfoMap;
	}

	public static void stopThread(String threadId)
	{
		Thread[] threads = ThreadUtil.findAllThreads();
		long idd = Long.valueOf(threadId).longValue();
		log.info("准备停止stop线程:" + threadId);
		for (int i = 0; i < threads.length; i++)
		{
			if (threads[i].getId() == idd)
			{
				System.out.println("找到线程开始停止:" + threadId + threads[i].getName() + threads[i].getClass());
				// threads[i].interrupt();
				threads[i].stop();
			}
		}

	}

	public static void interruptThread(String threadId)
	{
		Thread[] threads = ThreadUtil.findAllThreads();
		long idd = Long.valueOf(threadId).longValue();
		log.info("准备中断interrupt线程:" + threadId);
		for (int i = 0; i < threads.length; i++)
		{
			if (threads[i].getId() == idd)
			{
				System.out.println("找到线程开始中断:" + threadId + threads[i].getName() + threads[i].getClass());
				threads[i].interrupt();
			}
		}

	}
}
